const vscode = require('vscode');
/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {
	let disposable = vscode.commands.registerCommand('extension.insertLog', function () {

		const editor = vscode.window.activeTextEditor;
		const selection = editor.selection;
		if(selection.isEmpty) return vscode.window.showErrorMessage('请选择文本');
		let text = editor.document.getText(editor.selection);
		let logText = `console.log('${text} :>> ', ${text})`

		// 获取当前选中的行的文本
		const lineText = editor.document.lineAt(selection.start.line).text;
        // 获取当前选中的行的下一行
        const nextLine = editor.document.lineAt(selection.start.line + 1);
		// 获取空格
		let spaces = lineText.match(/^(\s+)/);
		let spacesStr = spaces ? spaces[0] : '';
        // 插入新的文本
        editor.edit((editBuilder) => {
            editBuilder.insert(nextLine.range.start, spacesStr + logText + '\n');
        });
	});

	context.subscriptions.push(disposable);
}

function deactivate() {}

module.exports = {
	activate,
	deactivate
}
